package com.bsh.controller.partner;

import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.bsh.dto.PartnerListQryDto;
import com.bsh.service.PartnerService;
import com.bsh.utils.IpUtil;
import com.bsh.utils.PageJosnMessage;
import com.bsh.utils.Pager;
import com.mysql.jdbc.StringUtils;

@Controller
public class PartnerController {
	private static Logger log = LoggerFactory.getLogger(PartnerController.class);

	@Autowired
	HttpServletRequest request;
	@Autowired
	HttpServletResponse response;
	@Autowired
	private PartnerService partService;

	/**
	 * 合作伙伴列表
	 */
	@RequestMapping(value = "/partner/list")
	public ModelAndView partnerList(@ModelAttribute PartnerListQryDto dto) {
		ModelAndView mv = new ModelAndView();
		mv.setViewName("/pages/partner/partnerList");

		String pageStr = request.getParameter("pageIndex");
		Integer pageIndex = Integer.parseInt(StringUtils.isNullOrEmpty(pageStr) ? "1" : pageStr);

		String sizeStr = request.getParameter("pageSize");
		Integer pageSize = Integer.parseInt(StringUtils.isNullOrEmpty(sizeStr) ? "10" : sizeStr);

		Pager pager = new Pager(pageIndex, pageSize);
		List<Map<String, Object>> list = partService.getPage(pager,dto);
		mv.addObject("list", list);
		mv.addObject("pager", pager);
		mv.addObject("qry", dto);
		return mv;
	}
	
	/**
	 * 作废
	 */
	@ResponseBody
	@RequestMapping(value = "/partner/disable/{partnerId}")
	public PageJosnMessage disable(@PathVariable(value="partnerId") Integer partnerId) {
		String ip=IpUtil.getIpAddr(request);
        Integer res =  partService.disable(partnerId);
        PageJosnMessage msg=new PageJosnMessage();
        res=(res==null?1:res);
        msg.putData("status",res>0?1:res);
		return msg;
	}
	
	/**
	 * 失效合作伙伴列表
	 */
	@RequestMapping(value = "/partner/removed/list")
	public ModelAndView partnerRemovedList(@ModelAttribute PartnerListQryDto dto) {
		ModelAndView mv = new ModelAndView();
		mv.setViewName("/pages/partner/partnerRemovedList");

		String pageStr = request.getParameter("pageIndex");
		Integer pageIndex = Integer.parseInt(StringUtils.isNullOrEmpty(pageStr) ? "1" : pageStr);

		String sizeStr = request.getParameter("pageSize");
		Integer pageSize = Integer.parseInt(StringUtils.isNullOrEmpty(sizeStr) ? "10" : sizeStr);

		Pager pager = new Pager(pageIndex, pageSize);
		List<Map<String, Object>> list = partService.getRemovedPage(pager,dto);
		mv.addObject("list", list);
		mv.addObject("pager", pager);
		mv.addObject("qry", dto);
		return mv;
	}
}
